package rc.database.connection;

/**
 * Created by IntelliJ IDEA.
 * User: Slejus
 * Date: 13.6.11
 * Time: 13.11
 * To change this template use File | Settings | File Templates.
 */

import org.json.JSONException;
import org.json.JSONObject;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;


public class DBOperations{
    private String username;
    private String password;
    private String jdbcUrl;
    private String jdbcDriver;

    private Properties properties;

    public void setProperties(Properties properties)
    {
        this.properties = properties;
        this.username = properties.getProperty("db.username");
        this.password = properties.getProperty("db.password");
        this.jdbcDriver = properties.getProperty("jdbc.driver");
        this.jdbcUrl = properties.getProperty("jdbc.url");
    }

    public Properties getProperties()
    {
        return properties;
    }

    public String getUsername()
    {
        if (username != null)
        {
           return username;
        } else
        {
            return "";
        }
    }

    public String getPassword()
    {
        if (password != null)
        {
            return password;
        } else
        {
            return "";
        }
    }

    public String getJdbcDriver()
    {
        if (jdbcDriver != null)
        {
            return jdbcDriver;
        } else
        {
            return "";
        }
    }

    public String getJdbcUrl()
    {
        if (jdbcUrl != null)
        {
            return jdbcUrl;
        } else
        {
            return "";
        }
    }

    public String getSuperior(int var_id){
        String query = "select jussle.getSuperior("+var_id+") from dual";
        System.out.println("QUERY: " +query);
        return query;
    }

    public String getSequenceNexVal(String sequence) {
        String query = "SELECT dokumentai.alf_" + sequence + ".NEXTVAL FROM DUAL";
        return query;
    }

    public String getJar2Query(String code) {
        String query = "select * from JAR2.V_JA_DUOMENYS where KODAS=" + code;
        return query;
    }

    public String getDepartmentsQuery(String code) {
        String query = "select PADN_ID, PADN_PAV from VART.PADALINIAI where PADN_PRIKLAUSO=" + code + " order by PADN_ID";
        return query;
    }

    public String getBranchesQuery() {
        String query = "select PADN_ID, PADN_PAV from VART.PADALINIAI where PADT_TIPAS IN (1,2) order by PADN_ID";
        return query;
    }

    public String getDocTypesQuery() {
        String query = "select DOKU_TIPAS, DOKU_PAV_I from DOKUMENTAI.DOKUMENTU_TIPAI ORDER BY DOKU_PAV_I asc";
        return query;
    }

    public JSONObject appendRsJSONData(JSONObject obj, ResultSet rs, String id, String title, String field1, String field2) throws SQLException, JSONException {
        if (rs.next()) {
            obj.append(id, rs.getString(field1));
            obj.append(title, rs.getString(field2));
            while (rs.next()) {
                obj.append(id, rs.getString(field1));
                obj.append(title, rs.getString(field2));
            }
        } else {
            obj.append(id, "None");
            obj.append(title, "None");
        }
        return obj;
    }

    public JSONObject appendTwoStrings(JSONObject obj, String id, String title, String set) throws JSONException {
        obj.append(id, set);
        obj.append(title, set);
        return obj;
    }

    public JSONObject appendTwoStrings(JSONObject obj, ResultSet rs, String id, String title, String field1, String field2) throws SQLException, JSONException {
        obj.append(id, rs.getString(field1));
        obj.append(title, rs.getString(field2));
        return obj;
    }

    public JSONObject appendDocTypesJSONData(JSONObject obj, ResultSet rs, String id, String title, String c1, String c2) throws SQLException, JSONException {
        while (rs.next()) {
            obj.append(id, rs.getString(c1));
            obj.append(title, rs.getString(c2));
        }
        return obj;
    }
}
